var city = require('../../utils/city.js');
var app = getApp()
Page({
  data: {
    searchLetter: [],
    showLetter: "",
    winHeight: 0,
    // tHeight: 0,
    // bHeight: 0,
    cityList: [],
    isShowLetter: false,
    scrollTop: 0, //置顶高度
    scrollTopId: '', //置顶id
    city: "深圳市",
    dw_city: '暂无定位信息',
    longitude: '',
    latitude: '',
    hotcityList: [{
      cityCode: 110000,
      city: '北京市'
    }, {
      cityCode: 310000,
      city: '上海市'
    }, {
      cityCode: 440100,
      city: '广州市'
    }, {
      cityCode: 440300,
      city: '深圳市'
    }, {
      cityCode: 330100,
      city: '杭州市'
    }, {
      cityCode: 320100,
      city: '南京市'
    }, {
      cityCode: 420100,
      city: '武汉市'
    }, {
      cityCode: 410100,
      city: '郑州市'
    }, {
      cityCode: 120000,
      city: '天津市'
    }, {
      cityCode: 610100,
      city: '西安市'
    }, {
      cityCode: 510100,
      city: '成都市'
    }, {
      cityCode: 500000,
      city: '重庆市'
    }]
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var searchLetter = city.searchLetter;
    var cityList = city.cityList();
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    var itemH = winHeight / searchLetter.length;
    var tempObj = [];
    for (var i = 0; i < searchLetter.length; i++) {
      var temp = {};
      temp.name = searchLetter[i];
      temp.tHeight = i * itemH;
      temp.bHeight = (i + 1) * itemH;
      tempObj.push(temp)
    }
    this.setData({
      winHeight: winHeight,
      itemH: itemH,
      searchLetter: tempObj,
      cityList: cityList,
      city: options.city || '深圳市'
    })
    this.dw_update()
  },
  clickLetter: function (e) {
    console.log(e.currentTarget.dataset.letter)
    var showLetter = e.currentTarget.dataset.letter;
    this.setData({
      showLetter: showLetter,
      isShowLetter: true,
      scrollTopId: showLetter,
    })
    var that = this;
    setTimeout(function () {
      that.setData({
        isShowLetter: false
      })
    }, 1000)
  },
  //选择当前城市
  bindNowCity() {
    if (this.data.city == this.data.dw_city) {
      wx.removeStorageSync('city')
    } else {
      wx.setStorageSync('city', this.data.city)
    }
    wx.setStorageSync('jg_city', this.data.city)
    wx.navigateBack()
  },
  // 选择定位城市
  bindNow_dw_city() {
    wx.removeStorageSync('city')
    wx.setStorageSync('jg_city', this.data.dw_city)
    wx.navigateBack()
  },
  // 重新定位
  dw_update() {
    this.getCity()
  },
  getCity() {
    console.log("执行点击到这了吗");
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        console.log("执行到这了吗1");
        this.setData({
          longitude: Number(res.longitude),
          latitude: Number(res.latitude),
        })
        this.reverse_geocoding(this.data.latitude, this.data.longitude)
      },
      fail: (e) => {
        console.log("执行到这了吗2");

        getApp().showModal('是否打开设置授权位置信息？', true).then(() => {
          wx.openSetting({
            success: (s) => {
              if (s.authSetting['scope.userLocation']) {
                this.getCity()
              }
            }
          });
        })
      }
    })
  },
  reverse_geocoding(latitude, longitude) {
    getApp().reverse_geocoding(latitude, longitude).then(result => {
      this.setData({
        dw_city: result.addressComponent.city
      });
    })
  },
  //选择城市
  bindCity: function (e) {
    console.log("bindCity")
    this.setData({
      city: e.currentTarget.dataset.city
    })
    wx.setStorageSync('city', this.data.city)
    wx.setStorageSync('jg_city', this.data.city)
    wx.navigateBack()
  },
  //选择热门城市
  bindHotCity: function (e) {
    console.log("bindHotCity")
    this.setData({
      city: e.currentTarget.dataset.city
    })
    wx.setStorageSync('city', this.data.city)
    wx.setStorageSync('jg_city', this.data.city)
    wx.navigateBack()
  },
  //点击热门城市回到顶部
  hotCity: function () {
    this.setData({
      scrollTop: 0,
    })
  }
})